package grimbo.goodwinfooty.server;

import grimbo.goodwinfooty.processing.CSV2UntypedListProcessor;
import grimbo.goodwinfooty.processing.PrintCSVProcessor;
import grimbo.goodwinfooty.processing.PrintUntypedListProcessor;
import grimbo.goodwinfooty.processing.Processor;
import grimbo.goodwinfooty.processing.UntypedList2FootyMatrixProcessor;
import grimbo.goodwinfooty.processing.XLS2CSVProcessor;
import grimbo.goodwinfooty.processing.XLS2InputStreamProcessor;
import grimbo.goodwinfooty.service.model.FootyMatrix;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FootyMatrixLoader {
    public FootyMatrixLoader() {

    }

    public FootyMatrix loadAsFootyMatrix(InputStream in) throws Exception {
        List processors = new ArrayList();
        processors.add(new XLS2InputStreamProcessor());
        processors.add(new XLS2CSVProcessor());
        processors.add(new PrintCSVProcessor());
        processors.add(new CSV2UntypedListProcessor());
        processors.add(new PrintUntypedListProcessor());
        processors.add(new UntypedList2FootyMatrixProcessor());

        Map context = new HashMap();
        context.put(XLS2InputStreamProcessor.CTX_XLS_INPUT_STREAM, in);

        for (int i = 0; i < processors.size(); i++) {
            Processor p = (Processor) processors.get(i);
            p.process(context);
        }

        return (FootyMatrix) context.get(UntypedList2FootyMatrixProcessor.CTX_FOOTY_MATRIX);
    }

    public List loadAsCSV(InputStream in) throws Exception {
        List processors = new ArrayList();
        processors.add(new XLS2InputStreamProcessor());
        processors.add(new XLS2CSVProcessor());
        processors.add(new CSV2UntypedListProcessor());

        Map context = new HashMap();
        context.put(XLS2InputStreamProcessor.CTX_XLS_INPUT_STREAM, in);

        for (int i = 0; i < processors.size(); i++) {
            Processor p = (Processor) processors.get(i);
            p.process(context);
        }

        return (List) context.get(CSV2UntypedListProcessor.CTX_UNTYPED_LIST);
    }
}
